/*
 * unittest1.c
 * unittest1 where I am attempting to make a set of random tests
 * to see cover as much of each function as possible.
 * This test will be for getWinners
 * I will exclude invalid input for now.
 * Help was from class examples, badTestDrawCard.c
 * and an example of ramdom testing from a previous class
 * found at http://code.google.com/p/cs362s2013/source/browse/trunk/lutzjo/dominion/unittest1.c
 *  Created on: Jan 25, 2014
 *      Author: Charles C. Clampitt
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "rngs.h"
#include <math.h>

#define DEBUG 0


// prototypes
int isGameOverTest(struct gameState *state);

 int isGameOverTest(struct gameState *state) {
   int i=0;
   int j=0;
   int numErrors = 0;
   int result = 0;
   int expectation = 0;
 //  int testNums = 0;
   struct gameState gsAfter;
   memcpy (&gsAfter, state, sizeof(struct gameState));

  //Check if stack of Province cards is empty. if so the game ends

  if (gsAfter.supplyCount[province]==0)
  {
          expectation = 1; //if province is 0,  expectation is 1
   }

   //check if three supply piles are empty. If so the game ends.
  // Test notes: At first I put this to 24 and I was able to generate 2-3 errors per run.
  // After I looked and saw that teh actual code in the isGameOver = 25 cards I no longer was
  // able to generate an error. Learning was that I could test that I was hitting that boundary
  // by leaving dropping this by one. Maybe I should set this to 24 or 26 just to see that code
  // coverage hits these and reports errors.
   j = 0;
   for (i = 0; i < 25; i++){
     //Take one from the province that we just tested
     if (gsAfter.supplyCount[i] == 0){  j++;  }
   }

   //If J is 3, game ends. Expectation is 1
   if ( j >= 3) {    expectation=1;   }

   // Call isGameOver in dominion and check result
   result = isGameOver(state);

   if(result != expectation){
     printf("Our result of %d did not match our expectations of %d \n",result, expectation);
     printf("Our paramaters at the time were gsAfter.supplyCount[province] = %d, gsAfter.supplyCount[i] = %d and J = %d \n", gsAfter.supplyCount[province], gsAfter.supplyCount[i], j);
     numErrors += 1; //did not match, add one to the error
   }
   if(memcmp(&gsAfter, state, sizeof(struct gameState)) != 0){
     printf("Our gameStates do not match! \n");
     numErrors += 1; //Gamestates did not match so add one to numErrors
   }

   return numErrors;

} // isGameOverTest




int main() {

   // Create main game play setup including structs,
  // variables, and any state I need to emulate

  struct gameState gameStateTest;//testing gameStateTest

  int x, y, z;
//  int playersTest[MAX_PLAYERS];
  int players;  // players
 // int minPlayers=2;
  int maxPlayers=4;
  int testNums = 0;
  int numErrors = 0;
 // int k[10] = {adventurer, council_room, feast, gardens, mine,
// 	       remodel, smithy, village, baron, great_hall};

  // Set the random number of players
  //players = (int)floor(Random() * maxPlayers);

 // now seed the game
  SelectStream(2);
  PutSeed(3);


  // Print to our screen which tests are being run
  // which will get directed to out .out file later
  // when we add commands to the makefile to redirect
  // the output

  printf("Testing isGameOverTest begin\n");
//  printf ("%d RANDOM TESTS.\n", testNums);

  // Additional setup and initialization. Some code
  // borrowed from badTestDrawCard.c

  for (x = 0; x < 4000; x++) {
      for (y = 0; y < sizeof(struct gameState); y++) {
           ((char*)&gameStateTest)[y] = floor(Random() * 256);
         }

      // Set the random number of players
       players = (int)floor(Random() * maxPlayers);

      // Setup my players decks and hands
       gameStateTest.deckCount[players] = floor(Random() * MAX_DECK);
       gameStateTest.discardCount[players] = floor(Random() * MAX_DECK);
       gameStateTest.handCount[players] = floor(Random() * MAX_HAND);



      for(z=0;z<gameStateTest.handCount[players];z++)
      {

    	  gameStateTest.hand[players][z] = (int)(Random() * MAX_HAND);

      }

      for(z=0;z<gameStateTest.discardCount[players];z++)
      	  {

         	  gameStateTest.discard[players][z] = (int)(Random() * MAX_HAND);

           }

      for(z=0; z<gameStateTest.deckCount[players]; z++)
          {

            gameStateTest.deck[players][z] = (int)(Random() * MAX_HAND);

          }

      //Randomly set supplyCount
          for (z = 0; z <= treasure_map; z++) {

        	  //gameStateTest.supplyCount[z] = (int)(Random() * 50 ); //most possible cards should be 50, (60-2*number of players)
        	  //gameStateTest.supplyCount[z] = (60 - (2* players) ) ; // Just checking upper boundary only covers 88%
        	  gameStateTest.supplyCount[z] = (int)(Random() * (60 - (2* players) ) );

          }


     // printf ("Initialization and setup completed now running test for isGameOverTest and counting errors.\n");
      numErrors += isGameOverTest(&gameStateTest);
      testNums += 1;
    }


  // If we get to here it means we kept running and did not crash the program
  // so our tests must have run now let's see how many errors we got.
    printf ("All Tests ran for isGameOverTest\n");
    printf ("%d RANDOM TESTS.\n", testNums);



if (numErrors > 0) { printf("There were %d errors found for isGameOverTest. ", numErrors);}
else { printf("There were no errors found in isGameOverTest.");}


return 0;

}  // End main



